﻿using System;
using System.Net;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Airport
{
    [DataContract]
    [KnownType(typeof(Node))]
    [KnownType(typeof(Gate))]
    [KnownType(typeof(Runway))]
    [KnownType(typeof(Taxiway))]
    public abstract class Surface
    {

        private int _surfaceId;
        private int _maxWeight;
        private bool _isOpen;
        private int _length;
        private int _width;
        private int _elevation;
        private string _name;
        private Node _endpoint1;
        private Node _endpoint2;
        private List<Node> _nodes;

        /// <summary>
        /// Default <c>Surface</c> constructor
        /// </summary>
        public Surface()
            : this(-1, "", -1, -1, -1, -2147483648)
        {
            // The elevation is set to the smallest int value
            _isOpen = false;  // Default to Closed if empty
            _nodes = new List<Node>();
        }

        public Surface(int id, string name, int maxWeight, int length, int width, int elevation)
        {
            _surfaceId = id;
            _name = name;
            _maxWeight = maxWeight;
            _length = length;
            _width = width;
            _elevation = elevation;
            _isOpen = true;   // Default to Open if explicitly created 
            _endpoint1 = new Node();
            _endpoint2 = new Node();
            _nodes = new List<Node>();
        }

        [DataMember]
        public int SurfaceId
        {
            get
            {
                return _surfaceId;
            }

            set
            {
                if (_surfaceId != value)
                {
                    _surfaceId = value;
                }
            }
        }

        [DataMember]
        public int MaxWeight
        {
            get
            {
                return _maxWeight;
            }

            set
            {
                if (_maxWeight != value)
                {
                    _maxWeight = value;
                }
            }
        }

        [DataMember]
        public bool IsOpen
        {
            get
            {
                return _isOpen;
            }

            set
            {
                if (_isOpen != value)
                {
                    _isOpen = value;
                }
            }
        }

        [DataMember]
        public int Length
        {
            get
            {
                return _length;
            }

            set
            {
                if (_length != value)
                {
                    _length = value;
                }
            }
        }

        [DataMember]
        public int Width
        {
            get
            {
                return _width;
            }

            set
            {
                if (_width != value)
                {
                    _width = value;
                }
            }
        }

        [DataMember]
        public int Elevation
        {
            get
            {
                return _elevation;
            }

            set
            {
                if (_elevation != value)
                {
                    _elevation = value;
                }
            }
        }

        [DataMember]
        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (_name != value)
                {
                    _name = value;
                }
            }
        }

        [DataMember]
        public Node Endpoint1
        {
            get
            {
                return _endpoint1;
            }

            set
            {
                if (_endpoint1 != value)
                {
                    _endpoint1 = value;
                }
            }
        }

        [DataMember]
        public Node Endpoint2
        {
            get
            {
                return _endpoint2;
            }

            set
            {
                if (_endpoint2 != value)
                {
                    _endpoint2 = value;
                }
            }
        }

        [DataMember]
        public List<Node> Nodes
        {
            get
            {
                return _nodes;
            }

            set
            {
                if (_nodes != value)
                {
                    _nodes = value;
                }
            }
        }
        
        public void AddNode(String nodeString, double scaleX = 1, double scaleY = 1)
        {
            _nodes.Add(this.parseNodeFromString(nodeString, scaleX, scaleY));
        }

        protected Node parseNodeFromString(String nodeString, double scaleX, double scaleY)
        {
            string[] strPoints = nodeString.Split(',');
            int[] points = new int[2];
            points[0] = (int)(Convert.ToInt32(strPoints[0]) * scaleX);
            points[1] = (int)(Convert.ToInt32(strPoints[1]) * scaleY);
            //return new Node(points[0], points[1]);
            
            // Do we need Node IDs?
            return new Node(-1, points[0], points[1]);
        }

    }
}
